package com.liu.frame.log;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Debug;

import com.liu.frame.config.Constant;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: baijianghai
 * 
 * @Description : 内存日志类
 * 
 * @Date: 2015年3月31日 上午10:26:01
 * 
 * @Version:2.0
 */
public class LogMemory {
	static String m_filePath; // 地址
	static String m_LogName; // 日志名

	/**
	 * 获取日志文件名
	 * 
	 * @return m_LogName 日志文件名
	 */
	private static String getLogName() {
		Calendar calendar = Calendar.getInstance();
		Date date = new Date(calendar.getTimeInMillis());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		m_LogName = format.format(date) + "_LogMemory.log";
		return m_LogName;
	}

	/**
	 * 记录日志信息
	 * 
	 * @param info
	 *            记录的日志信息
	 * @param context
	 */
	public static void write(Context context, String info) {
		String filePath = Constant.FILE_LOG_ERROR;
		try {
			File targetFile = new File(filePath + getLogName());
			if (!targetFile.exists()) {
				try {
					targetFile.getParentFile().mkdirs();
					targetFile.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			ProcessMemoryInfo processInfo = getRunningAppProcessInfo(context);
			m_filePath = filePath + getLogName();// log记录的地址
			RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateTime = sdf.format(Calendar.getInstance().getTime());
			String message = "";
			if (info != null && !info.equals("")) {
				message = dateTime + " " + info + " #消耗内存USS:"
						+ processInfo.uss + ",PSS:" + processInfo.pss + "\r\n";
			} else {
				message = dateTime + " #消耗内存USS:" + processInfo.uss + ",PSS:"
						+ processInfo.pss + "\r\n";
			}
			raf.seek(targetFile.length());
			raf.write(message.getBytes());
			raf.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除日志
	 */
	public static void clear() {
		File targetFile = new File(m_filePath);
		if (targetFile.exists()) {
			targetFile.delete();
			try {
				targetFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private static ProcessMemoryInfo getRunningAppProcessInfo(Context context) {

		String packageName = context.getPackageName();
		ActivityManager mActivityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);

		List<ActivityManager.RunningAppProcessInfo> appProcessList = mActivityManager
				.getRunningAppProcesses();

		ProcessMemoryInfo processInfo = new ProcessMemoryInfo();
		for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessList) {

			String processName = appProcessInfo.processName;
			if (processName != null && processName.equals(packageName)) {

				int pid = appProcessInfo.pid;
				int uid = appProcessInfo.uid;
				int[] myMempid = new int[] { pid };
				Debug.MemoryInfo[] memoryInfo = mActivityManager
						.getProcessMemoryInfo(myMempid);
				// int memSize = memoryInfo[0].dalvikPrivateDirty;
				int uss = memoryInfo[0].getTotalPrivateDirty(); // USS进程正在使用的内存大小
				int pss = memoryInfo[0].getTotalPss();
				int rss = memoryInfo[0].getTotalSharedDirty();

				System.out.println("内存# USS:" + uss + ",PSS:" + pss + ",RSS:"
						+ rss);
				processInfo.pid = pid;
				processInfo.uid = uid;
				processInfo.rss = rss;
				processInfo.pss = pss;
				processInfo.uss = uss;
				processInfo.processName = processName;
				break;
			}
		}

		return processInfo;
	}
}
